#!/bin/bash

# 判断是否设置环境变量 $ZBP_BASE_PATH
if [ -z "$ZBP_BASE_PATH" ]; then
    echo "环境变量 ZBP_BASE_PATH 未设置，请先阅读文档 https://alidocs.dingtalk.com/i/nodes/dQPGYqjpJYgo29QesdPED3yLWakx1Z5N?doc_type=wiki_doc "
    exit 1
fi

### 设置变量
# 软件安装根目录
INSTALL_DIR="$ZBP_BASE_PATH/zbp/software"
# 软件安装包下载地址
PACKAGE_URL="https://gitee.com/iamwx/deploy-files/raw/master/software/target/kkfileview_4.1.0.tar.gz"
# 下载后的安装包名称
PACKAGE_NAME="kkfileview_4.1.0.tar.gz"

echo "1. 初始化安装目录"
mkdir -p $INSTALL_DIR || { echo "无法创建安装目录: $INSTALL_DIR"; exit 1; }

echo "2. wget 校验"
if ! command -v wget &> /dev/null; then
    echo "wget 未安装，正在安装..."
    sudo yum install -y wget || { echo "wget 安装失败"; exit 1; }
fi

echo "3. 下载安装包到安装目录"
cd $INSTALL_DIR || { echo "无法进入安装目录: $INSTALL_DIR"; exit 1; }
wget $PACKAGE_URL -O $PACKAGE_NAME || { echo "下载安装包失败: $PACKAGE_URL"; exit 1; }

echo "4. 解压缩并进入目录"
tar -zxvf $PACKAGE_NAME || { echo "解压缩失败: $PACKAGE_NAME"; exit 1; }
EXTRACTED_DIR=$(tar -tzf $PACKAGE_NAME | head -1 | cut -f1 -d"/")
cd $EXTRACTED_DIR || { echo "无法进入解压后的目录: $EXTRACTED_DIR"; exit 1; }

echo "5. 清理安装包"
rm -rf ../$PACKAGE_NAME

echo "6. 执行安装脚本"
sh install.sh

echo "Install complete."